home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / src / smtp / smtpd.5.2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-28  |  2.6 KB  |  143 lines

  1. /* Server SMTP daemon */
  2.  
  3. #include "util.h"
  4. #include <fcntl.h>
  5. #include <signal.h>
  6. #include "netlib.h"
  7. #include "net/con.h"
  8. #include <stdio.h>
  9.  
  10. char *Channel;
  11. char *Server;
  12. int  Port;
  13. int  Maxconnections = 4;
  14. char *Network;
  15. int numconnections = 0;
  16.  
  17. extern int errno;
  18.  
  19. struct con openparam;
  20.  
  21. main (argc, argv)
  22. int argc;
  23. char **argv;
  24. {
  25.     register  netfid;
  26.     struct netstate statparam;
  27.     char *us, *them;
  28.     char *getuc ();
  29.     int i;
  30.     int pid;
  31.     int alarmtrap() ;
  32.  
  33.     if (argc != 6)
  34.     {
  35.         printf ("Usage:  %s server channels port maxconns network\n", argv[0]);
  36.         exit(1);
  37.     }
  38.  
  39.     Server = argv[1];
  40.     Channel = argv[2];
  41.     Port = atoi(argv[3]);
  42.     Maxconnections = atoi(argv[4]);
  43.     Network = argv[5];
  44.  
  45.     signal (SIGALRM, alarmtrap);
  46.  
  47.     close (2);
  48.     dup (1);            /* make log file standard error */
  49.     while (1)
  50.     {
  51.         openparam.c_mode = CONTCP;
  52.         openparam.c_lport = Port;
  53.         openparam.c_rbufs = 2;    /* Ask for 2K receive buffer */
  54.         if (isbadhost(openparam.c_lcon = gethost(Network)))
  55.             {
  56.             printf ("SMTP daemon cannot find network '%s'", Network);
  57.             exit (1);
  58.             }
  59.  
  60.         while ((netfid = open ("/dev/net", O_RDWR)) < 0
  61.             || ioctl(netfid, NETOPEN, &openparam) < 0)
  62.             {
  63.             close (netfid);
  64.             printf ("Return of %d from open, errno = %d\n", netfid, errno); 
  65.             fflush (stdout);
  66.             sleep (60);
  67.             }
  68.  
  69.         ioctl (netfid, NETSETE, O_RDONLY);
  70.  
  71. /*        ioctl (netfid, NETGETS, &statparam);
  72.  
  73.         printf ("Connection from %s\n", hostfmt(openparam.c_fcon,1));
  74. */
  75.         if(( pid = fork()) < 0 ) {
  76. /*            ll_log( "could not fork (%d)", errno);  */
  77.             (void) close( netfid );
  78.         } else if( pid == 0 ) {
  79.             /*
  80.              *  Child
  81.              */
  82.  
  83. /*            us = getuc (thisname());    /* who are we? */
  84.             us = getuc (Network);  /* daemon knows correct name */
  85.             ioctl (netfid, NETGETS, &statparam);
  86.             them = hostname (statparam.n_fcon); /* who are they? */
  87.             close (0);
  88.             dup (netfid);
  89.             close(1);
  90.             dup (netfid);
  91.             close (netfid);
  92.             execl (Server, Server, them, us, Channel, (char *)0);
  93.             exit (1);
  94.         }
  95.  
  96.  
  97.         /*
  98.          *  Parent
  99.          */
  100.         close (netfid);
  101.         numconnections++;
  102.  
  103.         /*
  104.          *  This code collects ZOMBIES and implements load
  105.          *  limiting by staying in the do loop while the
  106.          *  Maxconnections active.
  107.          */
  108.         if( numconnections > 1 || Maxconnections == 1 )
  109.             do {
  110.                 alarm( 2 );
  111.                 if( wait( &i ) > 0 )
  112.                     numconnections--;
  113.                 alarm( 0 );
  114.             } while( numconnections >= Maxconnections );
  115.  
  116.     }
  117.  
  118. }
  119.  
  120. /*
  121.  * Uppercase a string in place. Return pointer to
  122.  * null at end.
  123.  */
  124. char *
  125. getuc(s)
  126.     char *s;
  127. {
  128.     register char *p,
  129.           c;
  130.     for (p = s; c = *p; p++)
  131.     {
  132.     if (c <= 'z' && c >= 'a')
  133.         *p -= ('a' - 'A');
  134.     }
  135.     return(s);
  136. }
  137.  
  138. alarmtrap ()
  139. {
  140.  
  141.     signal (SIGALRM, alarmtrap);
  142. }
  143.